package com.uenpay.im.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.im.R;
import com.uenpay.im.entities.FileType;
import com.uenpay.im.entities.SendFile;

import java.util.ArrayList;

/**
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @date 2014/7/25
 * 
 */

public class DownloadedFileAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<SendFile> mList;

	public DownloadedFileAdapter(ArrayList<SendFile> _list, Context _context) {
		this.mContext = _context;
		this.mList = _list;
	}

	public void setSelectFile(int index) {
		for (int i = 0, lenght = mList.size(); i < lenght; i++) {
			mList.get(i).is_selected = 0;
		}
		mList.get(index).is_selected = 1;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public SendFile getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		SendFile file = getItem(position);
		if (convertView == null) {
			holder = new Holder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.download_file_list_item, parent,false);
			holder.nameTextView = (TextView) convertView
					.findViewById(R.id.download_file_name);
			holder.fileTypeName = (TextView) convertView
					.findViewById(R.id.file_type_name);
			holder.select_image = (ImageView) convertView
					.findViewById(R.id.download_select_file_image);
			holder.sizeTextView = (TextView) convertView
					.findViewById(R.id.download_file_size);
			holder.addDateTextView = (TextView) convertView
					.findViewById(R.id.download_file_add_date);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.select_image.setImageResource(R.drawable.icon_uncheck);

		if (file.is_selected == 1) {
			holder.select_image.setImageResource(R.drawable.icon_checked);
		}

		holder.fileTypeName.setText(FileType.getFileTypeName(file.fileName));
		holder.nameTextView.setText(file.fileName);
		holder.sizeTextView.setText(file.fileSize);
		holder.addDateTextView.setText(file.fileAddDate);
		return convertView;
	}

	class Holder {
		private ImageView select_image;
		private TextView sizeTextView, addDateTextView, nameTextView,fileTypeName;
	}

}
